package com.chijun.mform.field.view;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.chijun.mform.R;
import com.chijun.mform.create.field.IFieldInstallResult;
import com.chijun.mform.create.mode.FormConfig;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.field.BaseField;
import com.chijun.mform.field.FieldFullViewUtil;
import com.example.ccj.b.c.custom.TitleBarOnDisplayControl;
import com.example.ccj.b.c.dialogxy.SYDialog;
import com.example.ccj.b.c.draw.TopBar;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 字段设置
 */
public class DialogFieldInstall extends SYDialog.Builder {

    FormField formField;
    FormConfig formConfig;
    IFieldInstallResult fieldInstallResult;

    public DialogFieldInstall(Context context, FormField formField, FormConfig formConfig, IFieldInstallResult fieldInstallResult){
        super(context);
        this.formField = formField;
        this.formConfig = formConfig;
        this.fieldInstallResult = fieldInstallResult;
        setDialogView(R.layout.dialog_field_install)
                .setCancelable(true)
                .setScreenHeightP(1)
                .setScreenWidthP(1)
                .setWindowBackground(Color.WHITE);

    }

    @Override
    public void initView(View view) {
        super.initView(view);
        LinearLayout layout = view.findViewById(R.id.dialog_field_install_layout);
        TopBar topBar = view.findViewById(R.id.dialog_field_install_title);
        topBar.setTitle(formField.getName()+" 字段设置");
        topBar.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        topBar.setRightText("ok").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fieldInstallResult != null) fieldInstallResult.onModify(formField);
                dismiss();
            }
        });

        Observable.create((ObservableOnSubscribe<ViewGroup>) e -> {

                    BaseField baseField = FieldFullViewUtil.getFieldModelByType(formField.getType());
                    baseField.getFieldInstallView(view.getContext(),formConfig,formField,e);

                }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ViewGroup>() {
                    @Override
                    public void onSubscribe(Disposable d) {}

                    @Override
                    public void onNext(ViewGroup viewGroup) {
                        LogUtils.d("添加控件");
                        layout.addView(viewGroup,LinearLayout.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtils.e("onError: ", e.getMessage());
                        ToastUtils.showShort("设置项加载错误：", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }




}
